package com.csust.travel.mapper;

import com.csust.travel.entity.BookFavorites;
import com.csust.travel.entity.Books;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface BooksMapper {
    @Select("select * from books where hasPublish = 0 and owner =#{owner} order by update_time desc")
    List<Books> findPublish(Integer owner);

    @Select("select * from books where hasPublish = 1 and owner =#{owner} order by update_time desc")
    List<Books> findPublished(Integer owner);

    @Select("select * from books where hasPublish = 1  order by update_time desc")
    List<Books> findAllItem();

    @Select("<script>" +
            "SELECT * FROM books WHERE id IN " +
            "<foreach item='id' collection='item' open='(' separator=',' close=')'>" +
            "#{id}" +
            "</foreach>" +
            " ORDER BY update_time DESC" +
            "</script>")
    List<Books> findAllFavorites(@Param("item") List<Integer> item);


    @Select("select book_id from book_favorites where user_id = #{user_id}")
    List<Integer> getFavoritePage(Integer user_id);

    @Select("select * from book_favorites where user_id = #{user_id}")
    List<BookFavorites> getFavorites(Integer user_id);

    @Select("select * from books where author_nationality = #{nationality}")
    List<Books> findBooksByNationality(String nationality);

    @Update("update books set number = number - #{purchaseQuantity} where id = #{id}")
    void buyBooks(@Param("id") Integer id,@Param("purchaseQuantity") Integer purchaseQuantity);

    @Delete("delete from books where id = #{id}")
    void delItem(Integer id);

    @Update("update books set hasPublish = 1 where id = #{id}")
    void publishItem(Integer id);


    @Insert("INSERT INTO book_favorites (user_id, book_id) VALUES (#{user_id}, #{book_id})")
    void AddFavorite(Integer user_id, Integer book_id);


    @Delete("delete from book_favorites where user_id = #{user_id} and book_id = #{book_id}")
    void unHasFavorite(Integer user_id,Integer book_id);


    @Insert("insert into books values(null,#{book_code},#{name},#{isbn},#{author_nationality},#{applicable_scope},#{description},#{price},#{notes},#{type},#{number},#{book_image},now(),now(),#{owner},0)")
    void saveItem(Books item);

    @Select("select * from books where id = #{id}")
    Books findOne(Integer id);

    @Update("update books set book_code=#{book_code},name=#{name},isbn =#{isbn},author_nationality=#{author_nationality},applicable_scope=#{applicable_scope},description=#{description} ,price=#{price},notes=#{notes},type=#{type},number=#{number},book_image=#{book_image} where id = #{id}")
    void editItem(Books item);

    @Select("select * from books where book_code = #{string} or name like concat('%',#{string},'%') or isbn =#{string}")
    List<Books> findByQuery(String string);

    @Select("select * from books where (book_code = #{string} or name like concat('%',#{string},'%') or isbn =#{string}) and author_nationality = #{na}")
    List<Books> findByFilters(String string,String na);

    @Select("select * from books where owner = #{owner}")
    List<Books> findOwnItem(Integer owner);

    @Select("select number from books where id = #{id} ")
    Integer remainingQuantity(Integer id);

    @Select("select book_image from books where book_image is not null")
    List<String> findDb();

    @Insert("INSERT INTO statistics (user_id, bought)\n" +
            "VALUES (#{user_id}, 1)\n" +
            "ON DUPLICATE KEY UPDATE bought = bought + 1; ")
    void addBuy(Integer user_id);

    @Insert("INSERT INTO statistics (user_id, published)\n" +
            "VALUES (#{user_id}, 1)\n" +
            "ON DUPLICATE KEY UPDATE published = published + 1; ")
    void addPublish(Integer user_id);

    @Insert("INSERT INTO statistics (user_id, sold)\n" +
            "VALUES (#{user_id}, 1)\n" +
            "ON DUPLICATE KEY UPDATE sold = sold + 1; ")
    void addSold(Integer user_id);

    @Insert("INSERT INTO statistics (user_id, favorites)\n" +
            "VALUES (#{user_id}, 1)\n" +
            "ON DUPLICATE KEY UPDATE favorites = favorites + 1; ")
    void addFavorite(Integer user_id);


    @Update("update statistics set favorites = favorites -1 where user_id =#{user_id}")
    void removeFavorite(Integer user_id);
}
